package com.version1_2.dao.implement;


import com.SQLGroup.SQLAccess;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.property.Properties;
import com.version1_1.dao.ThirdPart.RongCloud.RongCloudDao;
import com.version1_1.dao.UserDao;
import com.version1_2.dao.UserDao1_2;
import com.version1_3.dao.SharedMethodDao1_3;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDao12Impl implements UserDao1_2 {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private SharedMethodDao1_3 sharedMethodDao13;


    /*创建log4j对象*/
    private static final Logger LOGGER = Logger.getLogger(UserDao12Impl.class);


    /**
     * @description: 更新用户关注列表
     */
    @Override
    public boolean updateFocusList(JSONObject updateJsonObject) {

        int userId = updateJsonObject.getIntValue("userId");
        JSONArray updateJsonArray = updateJsonObject
                .getJSONArray("updateFocusList");

        //获取用户现有关注列表
        String userFocusListString = "";

        try {
            userFocusListString = jdbcTemplate.queryForObject(SQLAccess.SelectUserFocusInfo,
                    String.class, new Object[]{userId});
        } catch (EmptyResultDataAccessException exception) {
            //获取错误
            LOGGER.error("更新用户关注列表,获取用户现有关注列表失败,userId= " + userId, exception);
            return false;
        }

        JSONObject userJsonObject = JSONObject.parseObject(userFocusListString);
        JSONArray userJsonArray = userJsonObject.getJSONArray("focusList");


        /*在更新用户关注内容时，同时改变分类关注数量*/
        //userDao.changeThirdFocusNum(updateJsonArray, userJsonArray);
        sharedMethodDao13.updateTypeSubscriNumber(updateJsonArray, "Android", userId); // 更改分类订阅数

        //循环更新用户关注内容
        for (int i = 0; i < updateJsonArray.size(); i++) {
            JSONObject updateJson = updateJsonArray.getJSONObject(i);
            int itemNumber = updateJson.getIntValue("itemNumber");
            for (int j = 0; j < userJsonArray.size(); j++) {
                JSONObject userJson = userJsonArray.getJSONObject(j);
                if (userJson.getIntValue("itemNumber") == itemNumber) {
                    userJsonArray.set(j, updateJson);
                }
            }
        }

        //更新用户关注数据库数据
        int result = jdbcTemplate.update(SQLAccess.UpdateUserFocusInfo,
                new Object[]{userJsonObject.toString(), userId});
        boolean updateResult = (result > 0 ? true : false);


        return updateResult;
    }

    /**
     * @description: 获取用户收藏列表
     */
    @Override
    public List getCollectList(int userId, int pageNumber) {

        List collectList = null;

        int count = 0;
        try {
            count = jdbcTemplate
                    .queryForObject(SQLAccess.SelectUserColleckCount, new Object[]{userId}, Integer.class);
        } catch (EmptyResultDataAccessException exception) {
            LOGGER.error("获取用户收藏列表,获取用户收藏列表长度失败...", exception);
            return collectList;
        }

        //请求分页数大于总页数
        if ((pageNumber - 1) * Properties.PageSize_Collect >= count) {
            LOGGER.info("获取用户收藏列表,用户请求分页数大于总页数...");
            return collectList;
        }

        return jdbcTemplate
                .queryForList(SQLAccess.SelectUserColleckListVersion1_2, new Object[]{userId, (pageNumber - 1) * Properties.PageSize_Collect, Properties.PageSize_Collect});
    }

    /**
     * @Description: 根据二级分类ID获取用户收藏中特定的二级分类的文章
     */
    @Override
    public List getSecondCollectList(int userId, int secondType, int pageNumber) {
        List secondCollectList = null;

        int count = 0;
        try {
            count = jdbcTemplate
                    .queryForObject(SQLAccess.SelectUserColleckCountOfSecondType, new Object[]{userId, secondType}, Integer.class);
        } catch (EmptyResultDataAccessException exception) {
            LOGGER.error("根据二级分类ID获取用户收藏中特定的二级分类的文章失败...", exception);
            return secondCollectList;
        }

        //请求分页数大于总页数
        if ((pageNumber - 1) * Properties.PageSize_Collect >= count) {
            LOGGER.error("获取用户收藏列表,用户请求分页数大于总页数...");
            return secondCollectList;
        }

        return jdbcTemplate
                .queryForList(SQLAccess.SelectUserCollerckListOfSecondTypeVersion1_2, new Object[]{userId, secondType, (pageNumber - 1) * Properties.PageSize_Collect, Properties.PageSize_Collect});
    }
}
